package org.example.controller;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.example.model.vo.UserVo;
import org.example.service.UserService;
import org.example.util.CommonRequest;
import org.example.util.CommonRequestHandle;
import org.example.util.JwtUtils;
import org.example.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "*",maxAge = 3600)
public class UserController {
    @Resource
    UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody CommonRequest commonRequest) {
        UserVo userVo = CommonRequestHandle.getRequest(commonRequest, UserVo.class);
        return userService.login(userVo);
    }

    @PostMapping("/add")
    public Result add(
            @RequestHeader("token") String token,
            @RequestBody CommonRequest commonRequest
    ) {
        UserVo userVo = CommonRequestHandle.getRequest(commonRequest, UserVo.class);
        return userService.add(token, userVo);
    }

    @GetMapping("/getCurrentUserInfo")
    public Result getCurrentInfo(@RequestHeader("token") String token) {
        System.out.println(1111);
        return JwtUtils.getUserInfo(token);
    }

    @GetMapping("/getUserById")
    public Result getUserById(Long Id) {
        return userService.getUserById(Id);
    }

    @GetMapping("/getUserListByIds")
    public Result getUserListByIds(List<Long> Ids) {
        return userService.getUserListByIds(Ids);
    }

    @GetMapping("/getPageUser")
    public Result getPageUser(
            @RequestParam(defaultValue = "1", required = false) Integer page,
            @RequestParam(defaultValue = "10", required = false, name = "limit") Integer size,
            UserVo userVo
    ) {
        return userService.getPageUser(page, size, userVo);
    }

    @GetMapping("/getUsersByRole")
    public Result getUserByRole(@RequestParam("role") String role) {
        return userService.getUsersByRole(role);
    }

    @PostMapping("/edit")
    public Result editUser(@RequestHeader("token") String token, @RequestBody CommonRequest commonRequest) {
        UserVo userVo = CommonRequestHandle.getRequest(commonRequest, UserVo.class);
        return userService.edit(token, userVo);
    }

    @GetMapping("/del")
    public Result delUser(Long id){
        return userService.deleteUserById(id);
    }


    @GetMapping("/switchStatus")
    public Result switchStatus(@Param("id") Long id, @Param("status") Long status){
        return userService.switchStatusById(id, status);
    }

    @GetMapping("/getUserCount")
    public Result getUserCount(){
        return userService.getUserCount();
    }
}
